﻿//// --------------------------------------------------------------------------------------------------------------------
//// <copyright company="DHGMS Solutions" file="ViewFilter.cs">
////   Licensed under GNU General Public License version 2 (GPLv2)
//// </copyright>
//// 
//// --------------------------------------------------------------------------------------------------------------------

//namespace Dhgms.Nucleotide.Model.Helper
//{
//    using System;
//    using System.Collections.Generic;
//    using System.Linq;
//    using System.Text;

//    using Dhgms.Nucleotide.Model.Info;
//    using Dhgms.Nucleotide.Model.Info.PropertyInfo;

//    /// <summary>
//    /// Helper for generating a view filter class
//    /// </summary>
//    public class ViewFilter : BaseClassGenerator
//    {
//        #region Constructors and Destructors

//        /// <summary>
//        /// Initializes a new instance of the <see cref="ViewFilter"/> class. 
//        /// Constructor
//        /// </summary>
//        public ViewFilter()
//            : base("Dhgms.DataManager.Model.Info.ViewFilter", "ViewFilter")
//        {
//        }

//        #endregion

//        #region Methods

//        /// <summary>
//        /// Gets the class suffix
//        /// </summary>
//        /// <returns>the class suffix</returns>
//        protected override string GetClassSuffix()
//        {
//            return "ViewFilter";
//        }

//        /// <summary>
//        /// Gets a collection of namespaces used by the code.
//        /// </summary>
//        /// <returns>Collection of namespaces</returns>
//        protected override IEnumerable<string> GetUsingNamespaces()
//        {
//            return new[]
//                       {
//                        "System",
//                        "System.ComponentModel.DataAnnotations",
//                        "System.Diagnostics",
//                        "System.Diagnostics.CodeAnalysis",
//                        "System.Runtime.Serialization",
//                        "System.Xml",
//                        "System.Xml.Linq"
//                       };
//        }

//        /// <summary>
//        /// Produces code for class specific methods
//        /// </summary>
//        /// <param name="sb">
//        /// The String Builder to add the code to
//        /// </param>
//        /// <param name="mainNamespaceName">
//        /// The main namespace
//        /// </param>
//        /// <param name="subNamespace">
//        /// The sub namespace, if any
//        /// </param>
//        /// <param name="className">
//        /// The class name
//        /// </param>
//        /// <param name="properties">
//        /// Properties of the info class
//        /// </param>
//        /// <param name="baseClassName">
//        /// The name of the base class
//        /// </param>
//        /// <param name="baseClassProperties">
//        /// The properties of the base class
//        /// </param>
//        protected override void DoClassSpecificMethods(
//            StringBuilder sb, 
//            string mainNamespaceName, 
//            string subNamespace, 
//            string className, 
//            Base[] properties, 
//            string baseClassName, 
//            Base[] baseClassProperties)
//        {
//            DoGetSelectedColumnNamesMethod(
//                sb, properties, baseClassProperties);
//        }

//        /// <summary>
//        /// The do constructor method.
//        /// </summary>
//        /// <param name="sb">
//        /// The String Builder to add the code to
//        /// </param>
//        /// <param name="className">
//        /// The class name
//        /// </param>
//        /// <param name="properties">
//        /// Properties of the info class
//        /// </param>
//        /// <param name="baseClassName">
//        /// The name of the base class
//        /// </param>
//        /// <param name="baseClassProperties">
//        /// The properties of the base class
//        /// </param>
//        protected override void DoConstructorMethod(
//            StringBuilder sb, string className, Base[] properties, string baseClassName, Base[] baseClassProperties)
//        {
//            // Constructor
//            sb.AppendLine(Helpers.GetAutoGeneratedWarning());
//            sb.AppendLine("        /// <summary>");
//            sb.AppendLine(
//                "        /// Initializes a new instance of the <see cref=\"" + className + this.GetClassSuffix()
//                + "\"/> class.");
//            sb.AppendLine("        /// </summary>");
//            sb.AppendLine("        public " + className + this.GetClassSuffix() + "() : base(1,0)");
//            sb.AppendLine("        {");
//            sb.AppendLine("        }");
//            sb.AppendLine(string.Empty);

//            // Constructor
//            sb.AppendLine(Helpers.GetAutoGeneratedWarning());
//            sb.AppendLine("        /// <summary>");
//            sb.AppendLine(
//                "        /// Initializes a new instance of the <see cref=\"" + className + this.GetClassSuffix()
//                + "\"/> class.");
//            sb.AppendLine("        /// </summary>");

//            if (string.IsNullOrWhiteSpace(baseClassName) == false && baseClassProperties != null
//                && baseClassProperties.Length > 0)
//            {
//                DoConstructorParamComments(sb, baseClassProperties);
//            }

//            DoConstructorParamComments(sb, properties);
//            sb.AppendLine("        /// <param name=\"pageNumber\">The number of the page being retrieved</param>");
//            sb.AppendLine("        /// <param name=\"pageSize\">The size of pages being retrieved</param>");

//            sb.AppendLine("        public " + className + this.GetClassSuffix() + "(");

//            var constructorParameters = properties.Select(
//                property => new ClrBoolean(CollectionType.None, property.Name, property.Description, false, property.IsKey, property.AlternativeDatabaseColumnName)).Cast<Base>().ToList();

//            constructorParameters.Add(new Integer32(CollectionType.None, "pageNumber", "Page Number", false, null, null, false, null));
//            constructorParameters.Add(new Integer32(CollectionType.None, "pageSize", "Page Size", false, null, null, false, null));

//            this.DoConstructorParameters(sb, constructorParameters.ToArray(), baseClassName, baseClassProperties);

//            sb.AppendLine("            : base(");

//            if (string.IsNullOrWhiteSpace(baseClassName) == false && baseClassProperties != null
//                && baseClassProperties.Length > 0)
//            {
//                int counter = 0;
//                while (counter < baseClassProperties.Length)
//                {
//                    sb.Append("                " + Helpers.GetVariableName(baseClassProperties[counter].Name) + ",");
//                    counter++;
//                }
//            }

//            sb.AppendLine("                pageNumber,");
//            sb.AppendLine("                pageSize");
//            sb.AppendLine("                )");
//            sb.AppendLine("        {");

//            foreach (Base pi in properties)
//            {
//                sb.AppendLine("            this." + pi.Name + " = " + Helpers.GetVariableName(pi.Name) + ";");
//            }

//            sb.AppendLine("        }");

//            sb.AppendLine(string.Empty);
//        }

//        /// <summary>
//        /// Builds the Fields Region
//        /// </summary>
//        /// <param name="sb">
//        /// The String Builder to add the code to
//        /// </param>
//        /// <param name="properties">
//        /// The collection of properties for the class
//        /// </param>
//        protected override void DoFieldsRegion(StringBuilder sb, Base[] properties)
//        {
//        }


//        /// <summary>
//        /// Generates the code for a column of the datarow
//        /// </summary>
//        /// <param name="sb">
//        /// string builder to append to
//        /// </param>
//        /// <param name="pi">
//        /// property information
//        /// </param>
//        protected void DoGetRowDataColumn(StringBuilder sb, Base pi)
//        {
//            sb.AppendLine(
//                "            " + pi.GetCSharpDataTypeDeclaration() + " " + Helpers.GetVariableName(pi.Name) + " = "
//                + pi.DefaultValue + ";");
//            sb.AppendLine("            if(" + Helpers.GetVariableName(pi.Name) + "Ordinal > -1)");
//            sb.AppendLine("            {");
//            sb.AppendLine(
//                "                " + Helpers.GetVariableName(pi.Name) + " = dataReader.Get" + pi.SqlDataReaderType + "("
//                + Helpers.GetVariableName(pi.Name) + "Ordinal);");
//            sb.AppendLine("            }");
//            sb.AppendLine(string.Empty);
//        }


//        /// <summary>
//        /// Builds the Properties Region
//        /// </summary>
//        /// <param name="sb">
//        /// The String Builder to add the code to
//        /// </param>
//        /// <param name="properties">
//        /// The collection of properties for the class
//        /// </param>
//        /// <param name="baseClassProperties">
//        /// The properties of the base class
//        /// </param>
//        protected override void DoPropertiesRegion(StringBuilder sb, Base[] properties, Base[] baseClassProperties)
//        {
//            sb.AppendLine("        #region properties");

//            foreach (Base pi in properties)
//            {
//                sb.AppendLine("        /// <summary>");
//                sb.AppendLine("        /// " + pi.Description);
//                sb.AppendLine("        /// </summary>");

//                sb.AppendLine("        public bool " + pi.Name);
//                sb.AppendLine("        {");
//                sb.AppendLine("            get;");
//                sb.AppendLine("            set;");
//                sb.AppendLine("        }");
//                sb.AppendLine(string.Empty);
//            }

//            DoGetHeaderRecordMethod(sb, properties, baseClassProperties);

//            this.OnDoClassSpecificProperties(sb, properties);

//            sb.AppendLine("        #endregion");
//            sb.AppendLine(string.Empty);
//        }

//        /// <summary>
//        /// Gets the type to be used for a parameter in the constructor
//        /// </summary>
//        /// <param name="propertyInfo">
//        /// the property
//        /// </param>
//        /// <returns>
//        /// the constructor parameter type
//        /// </returns>
//        protected override string GetConstructorParameterType(Base propertyInfo)
//        {
//            return propertyInfo.NetDataType;
//        }

//        /// <summary>
//        /// Gets the type for a property
//        /// </summary>
//        /// <param name="pi">
//        /// The property to generate a type for
//        /// </param>
//        /// <returns>
//        /// .NET data type
//        /// </returns>
//        protected override string GetPropertyType(Base pi)
//        {
//            return "bool";
//        }

//        /// <summary>
//        /// Gets the value of the property when converting to a string array.
//        /// </summary>
//        /// <param name="pi">
//        /// property information
//        /// </param>
//        /// <returns>
//        /// the value of the property when converting to a string array
//        /// </returns>
//        protected override string GetValueForToStringArray(Base pi)
//        {
//            if (pi == null)
//            {
//                throw new ArgumentNullException("pi");
//            }

//            return pi.Name + "? \"1\" : \"0\"";
//        }

//        /// <summary>
//        /// Does any class specific properties. For example the count property on the difference class
//        /// </summary>
//        /// <param name="sb">
//        /// The String Builder to add the code to
//        /// </param>
//        /// <param name="properties">
//        /// The collection of properties for the class
//        /// </param>
//        protected override void OnDoClassSpecificProperties(StringBuilder sb, Base[] properties)
//        {
//        }

//        /// <summary>
//        /// Generates the code for comparing properties in the CompareTo method
//        /// </summary>
//        /// <param name="sb">
//        /// StringBuilder to add the code to
//        /// </param>
//        /// <param name="properties">
//        /// The property to generate code for
//        /// </param>
//        /// <param name="baseClassProperties">
//        /// Properties in the base class, if any
//        /// </param>
//        /// <param name="checkResultDeclared">
//        /// Whether the check result variable has been declared yet
//        /// </param>
//        protected override void OnDoCompareToProperties(
//            StringBuilder sb, Base[] properties, Base[] baseClassProperties, bool checkResultDeclared)
//        {
//            int returnResultCounter = (baseClassProperties == null) ? 1 : baseClassProperties.Length + 1;

//            foreach (Base pi in properties)
//            {
//                sb.Append("            ");
//                if (!checkResultDeclared)
//                {
//                    sb.Append("int ");
//                }

//                sb.AppendLine("checkResult = " + pi.Name + ".CompareTo(other." + pi.Name + ");");
//                sb.AppendLine("            if(checkResult != 0)");
//                sb.AppendLine("            {");
//                sb.AppendLine(
//                    "                return (checkResult > 0) ? " + returnResultCounter + " : -" + returnResultCounter + ";");
//                sb.AppendLine("            }");
//                sb.AppendLine(string.Empty);

//                if (!checkResultDeclared)
//                {
//                    checkResultDeclared = true;
//                }

//                returnResultCounter++;
//            }
//        }

//        /// <summary>
//        /// Generates the field code for a property member
//        /// </summary>
//        /// <param name="sb">
//        /// The String Builder to add the code to
//        /// </param>
//        /// <param name="pi">
//        /// The property to generate a field for
//        /// </param>
//        protected override void OnDoFieldItem(StringBuilder sb, Base pi)
//        {
//            if (sb == null)
//            {
//                throw new ArgumentNullException("sb");
//            }

//            if (pi == null)
//            {
//                throw new ArgumentNullException("pi");
//            }

//            sb.AppendLine("            " + pi.GetCSharpDataTypeDeclaration() + " _" + pi.Name + ";");
//        }

//        /// <summary>
//        /// Generates the mutator for the property
//        /// </summary>
//        /// <param name="sb">
//        /// The String Builder to add the code to
//        /// </param>
//        /// <param name="pi">
//        /// The property to generate the mutator for
//        /// </param>
//        protected override void OnDoPropertyMutator(StringBuilder sb, Base pi)
//        {
//            if (sb == null)
//            {
//                throw new ArgumentNullException("sb");
//            }

//            if (pi == null)
//            {
//                throw new ArgumentNullException("pi");
//            }

//            sb.AppendLine("            set");
//            sb.AppendLine("            {");
//            sb.AppendLine("                _" + pi.Name + " = value;");
//            sb.AppendLine("            }");
//        }

//        /// <summary>
//        /// Generates the GetDifferences method
//        /// </summary>
//        /// <param name="sb">
//        /// StringBuilder to add the code to
//        /// </param>
//        /// <param name="properties">
//        /// Properties of the info class
//        /// </param>
//        /// <param name="baseClassProperties">
//        /// The properties of the base class
//        /// </param>
//        private static void DoGetSelectedColumnNamesMethod(
//            StringBuilder sb, 
//            IEnumerable<Base> properties, 
//            ICollection<Base> baseClassProperties)
//        {
//            sb.AppendLine("        /// <summary>");
//            sb.AppendLine("        /// Gets a comma separated list of column names");
//            sb.AppendLine("        /// </summary>");
//            sb.AppendLine("        /// <returns>a comma separated list of column names</returns>");
//            sb.AppendLine("        public override string GetSelectedColumnNames()");
//            sb.AppendLine("        {");

//            sb.AppendLine("            System.Text.StringBuilder sb = new System.Text.StringBuilder();");

//            bool doCommaCode = false;
//            if (baseClassProperties != null && baseClassProperties.Count > 0)
//            {
//                sb.AppendLine("            sb.Append(base.GetSelectedColumnNamesMethod());");
//                sb.AppendLine("            string comma = (sb.Length > 0) ? \",\" : null;");
//                doCommaCode = true;
//            }
//            else
//            {
//                sb.AppendLine("            string comma = null;");
//            }

//            foreach (Base pi in properties)
//            {
//                sb.AppendLine("            // " + pi.Name);
//                sb.AppendLine("            if (" + pi.Name + ")");
//                sb.AppendLine("            {");
//                if (doCommaCode)
//                {
//                    sb.AppendLine("                sb.AppendLine(comma + \"" + pi.Name + "\");");
//                    sb.AppendLine(string.Empty);
//                    sb.AppendLine("                if(comma == null)");
//                    sb.AppendLine("                {");
//                    sb.AppendLine("                    comma = \",\";");
//                    sb.AppendLine("                }");
//                }
//                else
//                {
//                    sb.AppendLine("                sb.AppendLine(\"" + pi.Name + "\");");
//                    sb.AppendLine(string.Empty);
//                    sb.AppendLine("                comma = \",\";");
//                    doCommaCode = true;
//                }

//                sb.AppendLine("            }");
//            }

//            sb.AppendLine("            return sb.ToString();");

//            sb.AppendLine("        }");
//            sb.AppendLine(string.Empty);
//        }

//        /*
//        protected override void DoGetStronglyTypedObjectFromDataReaderRowMethod(
//            StringBuilder sb, string className, Base[] properties, string baseClassName, Base[] baseClassProperties)
//        {
//            sb.AppendLine("        /// <summary>");
//            sb.AppendLine("        /// Get Strongly Typed Object from a data reader");
//            sb.AppendLine("        /// </summary>");
//            string modifier = (!string.IsNullOrWhiteSpace(baseClassName) && baseClassProperties != null
//                               && baseClassProperties.Length > 0)
//                                  ? "new "
//                                  : "override ";
//            sb.AppendLine("        public " + modifier + className + this.GetClassSuffix() + " GetStronglyTypedObjectFromDataReaderRow(System.Data.Helpers.DbDataReader dataReader)");
//            sb.AppendLine("        {");
//            sb.AppendLine("            if(dataReader == null)");
//            sb.AppendLine("            {");
//            sb.AppendLine("                throw new System.ArgumentNullException(\"dataReader\");");
//            sb.AppendLine("            }");
//            sb.AppendLine(string.Empty);

//            sb.AppendLine("            //get ordinals");
//            if (baseClassProperties != null && baseClassProperties.Length > 0)
//            {
//                DoGetOrdinals(sb, baseClassProperties);
//            }

//            DoGetOrdinals(sb, properties);
//            sb.AppendLine("            int pageNumberOrdinal = dataReader.GetOrdinal(\"pageNumber\");");
//            sb.AppendLine("            int pageSizeOrdinal = dataReader.GetOrdinal(\"pageSize\");");
//            sb.AppendLine(string.Empty);

//            sb.AppendLine("            //get row data");
//            if (baseClassProperties != null && baseClassProperties.Length > 0)
//            {
//                this.DoGetRowData(sb, baseClassProperties);
//            }

//            this.DoGetRowData(sb, properties);
//            this.DoGetRowDataColumn(sb, this.pageNumberPropertyInformation);
//            this.DoGetRowDataColumn(sb, this.pageSizePropertyInformation);
//            sb.AppendLine(string.Empty);

//            sb.AppendLine("            return new " + className + this.GetClassSuffix() + "(");
//            if (baseClassProperties != null && baseClassProperties.Length > 0)
//            {
//                sb.AppendLine("                " + Helpers.GetVariableName(baseClassProperties[0].Name));
//                for (int i = 1; i < baseClassProperties.Length; i++)
//                {
//                    sb.AppendLine("                ," + Helpers.GetVariableName(baseClassProperties[i].Name));
//                }
//            }

//            string comma = (baseClassProperties != null && baseClassProperties.Length > 0) ? "," : null;
//            sb.AppendLine("                " + comma + Helpers.GetVariableName(properties[0].Name));
//            for (int i = 1; i < properties.Length; i++)
//            {
//                sb.AppendLine("                ," + Helpers.GetVariableName(properties[i].Name));
//            }

//            sb.AppendLine("                ,pageNumber");
//            sb.AppendLine("                ,pageSize");
//            sb.AppendLine("                );");
//            sb.AppendLine("        }");
//            sb.AppendLine(string.Empty);
//        }
//         * */
//        #endregion
//    }
//}